package com.salelogistics.basic.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.salelogistics.basic.domain.vo.TreeSelect;
import com.salelogistics.common.core.utils.StringUtils;
import com.salelogistics.system.api.domain.SysDept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.salelogistics.basic.mapper.BasMaterialTypeMapper;
import com.salelogistics.basic.domain.BasMaterialType;
import com.salelogistics.basic.service.IBasMaterialTypeService;

/**
 * 材料类型Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-14
 */
@Service
public class BasMaterialTypeServiceImpl implements IBasMaterialTypeService 
{
    @Autowired
    private BasMaterialTypeMapper basMaterialTypeMapper;
    Integer num=1;



    /**
     * 查询材料类型
     * 
     * @param materialTypeId 材料类型主键
     * @return 材料类型
     */
    @Override
    public BasMaterialType selectBasMaterialTypeByMaterialTypeId(Long materialTypeId)
    {
        return basMaterialTypeMapper.selectBasMaterialTypeByMaterialTypeId(materialTypeId);
    }

    /**
     * 查询材料类型列表
     * 
     * @param basMaterialType 材料类型
     * @return 材料类型
     */
    @Override
    public List<BasMaterialType> selectBasMaterialTypeList(BasMaterialType basMaterialType)
    {
        return basMaterialTypeMapper.selectBasMaterialTypeList(basMaterialType);
    }

    /**
     * 新增材料类型
     * 
     * @param basMaterialType 材料类型
     * @return 结果
     */
    @Override
    public int insertBasMaterialType(BasMaterialType basMaterialType)
    {
//        basMaterialType.setCreateTime(DateUtils.getNowDate());
        return basMaterialTypeMapper.insertBasMaterialType(basMaterialType);
    }

    /**
     * 修改材料类型
     * 
     * @param basMaterialType 材料类型
     * @return 结果
     */
    @Override
    public int updateBasMaterialType(BasMaterialType basMaterialType)
    {
        return basMaterialTypeMapper.updateBasMaterialType(basMaterialType);
    }

    /**
     * 批量删除材料类型
     * 
     * @param materialTypeIds 需要删除的材料类型主键
     * @return 结果
     */
    @Override
    public int deleteBasMaterialTypeByMaterialTypeIds(Long[] materialTypeIds)
    {
        return basMaterialTypeMapper.deleteBasMaterialTypeByMaterialTypeIds(materialTypeIds);
    }

    /**
     * 删除材料类型信息
     * 
     * @param materialTypeId 材料类型主键
     * @return 结果
     */
    @Override
    public int deleteBasMaterialTypeByMaterialTypeId(Long materialTypeId)
    {
        return basMaterialTypeMapper.deleteBasMaterialTypeByMaterialTypeId(materialTypeId);
    }



    @Override
    public List<TreeSelect> selectTypeTreeList(BasMaterialType basMaterialType) {
        List<BasMaterialType> types = selectBasMaterialTypeList(basMaterialType);
        return buildTypeTreeSelect(types);
    }


    /**
     * 构建前端所需要树结构
     *
     * @param types 材料类型列表
     * @return 树结构列表
     */
    @Override
    public List<BasMaterialType> buildTypeTree(List<BasMaterialType> types)
    {
        List<BasMaterialType> returnList = new ArrayList<>();
        List<Long> tempList = types.stream().map(BasMaterialType::getMaterialTypeId).collect(Collectors.toList());
        for (BasMaterialType type : types)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(type.getParentId()))
            {
                recursionFn(types, type);
                returnList.add(type);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = types;
        }
        return returnList;
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param types 部门列表
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelect> buildTypeTreeSelect(List<BasMaterialType> types)
    {
        List<BasMaterialType> typeTrees = buildTypeTree(types);
        return typeTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }



    /**
     * 递归列表
     */
    private void recursionFn(List<BasMaterialType> list, BasMaterialType t)
    {
        // 得到子节点列表
        List<BasMaterialType> childList = getChildList(list, t);
        t.setChildren(childList);
        for (BasMaterialType tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<BasMaterialType> getChildList(List<BasMaterialType> list, BasMaterialType t)
    {
        List<BasMaterialType> tlist = new ArrayList<>();
        Iterator<BasMaterialType> it = list.iterator();
        while (it.hasNext())
        {
            BasMaterialType n = it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getMaterialTypeId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<BasMaterialType> list, BasMaterialType t)
    {
        return getChildList(list, t).size() > 0  ? true : false;
    }


    /**
     * 查询是否有子节点
     */
    @Override
    public List<BasMaterialType> selectTypeParent(Long typeId) {
        return basMaterialTypeMapper.selectTypeParent(typeId);
    }
}
